package com.feihao.zhou.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.feihao.zhou.bean.Employee;
import com.feihao.zhou.service.EmployeeService;
import com.feihao.zhou.service.impl.EmployeeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/empServlet")
public class EmpServlet extends HttpServlet{
    static EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("find".equals(operation)){
            find(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }else if ("toUpdate".equals(operation)){
            toUpdate(req,resp);
        }else if ("update".equals(operation)){
            update(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }


    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Employee employee = new Employee();
        try {
            BeanUtils.populate(employee,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        employeeService.addEmployee(employee);

        find(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Employee employee = new Employee();
        try {
            BeanUtils.populate(employee,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        employeeService.updateEmployee(employee);

        //更新完成将operation置为save;
        req.setAttribute("operation","save");

        find(req,resp);
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String empId = req.getParameter("empId");
        Employee employee = employeeService.findById(empId);
        req.setAttribute("emp",employee);
        req.setAttribute("operation","update");

        find(req,resp);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String empId = req.getParameter("empId");
        employeeService.deleteEmployee(empId);
        //再次调用查询方法：
        find(req,resp);
    }

    private void find(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        List<Employee> employeeList = employeeService.findAll();
//        System.out.println(employeeList);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(employeeList);
        System.out.println(json);
        req.setAttribute("empList",employeeList);

        //查询完后，默认按钮设置operation为save:
        Object operation = req.getAttribute("operation");
        if (operation==null){
            req.setAttribute("operation","save");
        }

        //
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
